package org.zoomdev.zoom.web.controller.test;

import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.util.concurrent.atomic.AtomicInteger;

public class Monitor {

    private final AtomicInteger num = new AtomicInteger(0);

    private Object[] arguments;
    private HttpServletResponse response;
    private Throwable error;
    private Method method;

    public void increase() {
        num.incrementAndGet();
    }

    public void setArguments(HttpServletResponse response, Object... arguments) {
        this.arguments = arguments.clone();
        num.incrementAndGet();
        this.response = response;
        this.error = null;
    }

    public void setError(HttpServletResponse response, Throwable error) {
        num.incrementAndGet();
        this.response = response;
        this.error = error;
        this.arguments = null;
    }

    public Object[] arguments() {
        if (error != null) {
            throw new RuntimeException(error);
        }
        return arguments;
    }

    public int count() {
        return num.get();
    }

    public HttpServletResponse getResponse() {
        return response;
    }

    public Throwable getError() {
        return error;
    }

    public void setError(Throwable error) {
        this.error = error;
    }

    public Method getMethod() {
        return method;
    }

    public void setMethod(Method method) {
        this.method = method;
    }
}
